package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.admin.service.MerchantService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.MerchantDto;
import com.huilian.iotbox.data.enums.ErrorMsgEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * @author 龙 路 飞
 * @CreateDate: 2021-10-20 16:29
 * @Description:
 */
@RestController
@RequestMapping("/api/merchant")
public class MerchantController extends BaseController {
    @Autowired
    private MerchantService merchantService;
    @Resource
    private UserService userService;
    @PostMapping("/findList")
    public ServerResponse findList(@RequestBody MerchantDto merchantDto,HttpServletRequest request){
        merchantDto.setPageNum(Integer.parseInt(request.getParameter("pageNum")));
        merchantDto.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
        return ServerResponse.createBySuccess(merchantService.findList(merchantDto));
    }

    /**切换商户渠道*/
    @PostMapping("/switchoverPayCompany")
    public ServerResponse switchoverPayCompany(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception{
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if(ObjectUtils.isEmpty(userInfo)){
            return ServerResponse.createByErrorMessage(ErrorMsgEnum.USER_INFO_ERROR.getMsg());
        }
        return merchantService.switchoverPayCompany(merchantDto);
    }

    @PostMapping("/findAuthenticationStatusById")
    public ServerResponse findAuthenticationStatusById(@RequestBody MerchantDto merchantDto,HttpServletRequest request) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if(ObjectUtils.isEmpty(userInfo)){
            return ServerResponse.createByErrorMessage(ErrorMsgEnum.USER_INFO_ERROR.getMsg());
        }
        return ServerResponse.createBySuccess(merchantService.findAuthenticationStatusById(merchantDto.getId()));
    }
}
